package com.example.smartf.controller;

import com.example.smartf.model.mysql.Formulainfo;
import com.example.smartf.model.mysql.Traindata;
import com.example.smartf.repository.mysql.TraindataRepository;
import com.example.smartf.service.TraindataService;
import com.example.smartf.tool.PythonServer;
import io.swagger.annotations.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * 训练数据 -即 TrainData 的控制层
 */
@Api("设备训练数据")
@CrossOrigin(origins = "*")
@RestController
public class TraindataController {

    @Autowired
    private TraindataService traindataService;


    @ApiOperation("显示单个设备的 所有训练数据")
    @ApiImplicitParam(name = "deviceID", value = "设备ID", dataType = "String",paramType = "query")
    @PostMapping(value = "/traindata/showall")
    List<Traindata> showTrainData(@RequestParam("deviceID") String deviceID){
        return  traindataService.showdeviceTraindata(deviceID);
    }

    @ApiOperation("标注一个设备一天的训练数据 结果 即Cgr 生长率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceID", value = "设备ID", dataType = "String" , paramType = "query"),
            @ApiImplicitParam(name = "day" , value = "标记哪一天", dataType = "String" , paramType = "query"),
            @ApiImplicitParam(name = "Cgr", value = "作物一天的生长率" ,dataType = "String",paramType = "query")
    })
    @PostMapping(value = "/traindata/updatecgr")
    String updateTrainDataCgr(@RequestParam("deviceID") String deviceID,
                              @RequestParam("day") String day,
                              @RequestParam("Cgr") String Cgr){
        return traindataService.updatedeviceCgr(deviceID,Integer.parseInt(day),Double.parseDouble(Cgr));
    }


    @ApiOperation("选择 哪几天 进行机器学习")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "deviceID",value = "设备ID",dataType = "String",paramType = "query")
    })
    @PutMapping(value = "/traindata/starttrain")
    void startTrain(@RequestParam("username") String username,
                    @RequestParam("deviceID") String deviceID,
                    @RequestParam("days") @ApiParam(value = "天数列表") List<Integer> days){
        traindataService.startTrain(username,deviceID,days);
    }

    //上传文件
    @ApiOperation("上传Excel文件进行机器学习，生成一个公式")
    @PostMapping(value = "/traindata/uploadexcel",consumes = "multipart/*",headers = "content-type=multipart/form-data")
    String upLoadExcel(@ApiParam(value = "上传的文件",required = true) MultipartFile file) throws Exception{
        if(file == null || file.isEmpty()){
            return "empty";
        }
        if(file.getSize() > 4194304){
            return "too big";
        }
        InputStream inputStream = file.getInputStream();
        //取 文件名
        String filename = file.getOriginalFilename();
        Workbook workbook = null;
        // 取 后缀名
        String fileType = filename.substring(filename.lastIndexOf("."));

        if(".xls".equals(fileType) == true){
            workbook = new HSSFWorkbook(inputStream);
        }else if( ".xlsx".equals(fileType) == true){
            workbook = new XSSFWorkbook(inputStream);
        }else{
            workbook = null;
            return "wrong file type";
        }

        if (null == workbook){
            return "try again";
        }
        return traindataService.upLoadExcel(workbook,filename);
    }

    @ApiOperation("将公式应用于训练数据的标注")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "作物种类",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "formula",value = "计算公式",dataType = "String",paramType = "query")
    })
    @PutMapping(value = "/traindata/applyformula")
    void applyFormula(@RequestParam("type") String type,
                     @RequestParam("formula") String formula){
        traindataService.applyFormula(type,formula);
    }

    @ApiOperation("显示所有计算公式")
    @ApiImplicitParam(name = "type",value = "作物种类",dataType = "String",paramType = "query")
    @PostMapping(value = "/traindata/showformulas")
    List<Formulainfo> showformulas(@RequestParam("type") String type){
        return traindataService.showFormulas(type);
    }

    @ApiOperation("删除一个公式")
    @ApiImplicitParam(name = "id",value = "公式ID",dataType = "String",paramType = "query")
    @PostMapping(value = "/traindata/delformula")
    String delformula(@RequestParam("id") String id){
        return traindataService.delFormula(Integer.parseInt(id));
    }

}
